package keyboard.gui;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.Timer;

import keyboard.model.Keyboard;
import main.model.Settings;

@SuppressWarnings("serial")
public class TextBox extends JComponent implements ActionListener {
	
	private boolean hiddenChars;
	private String header;
	private boolean active;
	private boolean activeState;
	private Keyboard keyboard;
	
	public TextBox(Keyboard keyboard, boolean hiddenChars) {
		this.keyboard = keyboard;
		active = false;
		this.hiddenChars = hiddenChars;
		
		Settings settings = new Settings();
		if (hiddenChars)
			header = settings.keyboardPassword();
		else
			header = settings.keyboardText();
		
		Timer t = new Timer(500,this);
		t.start();
	}
	
	@Override
	public void paint(Graphics g) {
		
		g.setFont(new Font("Arial", Font.PLAIN, 20));
		FontMetrics fm = g.getFontMetrics();
		Rectangle r = getBounds();
		r.height = 4*fm.getHeight();
		setBounds(r);
		g.drawString(header, 10, fm.getHeight());
		g.drawRect(0, (int)(fm.getHeight()*1.5), getBounds().width-1, (int)(fm.getHeight()*1.5));
		String text = "";
		if (hiddenChars) {
			text = keyboard.getPassword();
		} else {
			text = keyboard.getUsername();
		}
		g.drawString(text, 5, (int)(fm.getHeight()*2.75));
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (active) {
			activeState = !activeState;
		} else {
			activeState = false;
		}
		repaint();
	}
}
